package vn.allmobile.ambnews.activities.list;

import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.activities.category.CategoryActivity;
import vn.allmobile.ambnews.customview.LoadingDialog;
import vn.allmobile.ambnews.data.Category;
import vn.allmobile.ambnews.data.Paper;
import vn.allmobile.ambnews.network.IncommingRequestListenter;
import vn.allmobile.ambnews.network.LoadDataListener;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.GlobalApplication;
import vn.allmobile.ambnews.utils.Log;
import vn.allmobile.ambnews.utils.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

public class onUserPaperClickListenner implements OnClickListener,LoadDataListener  {
	int _itemIndex;
	LoadingDialog _dialog;
	public Activity _activity;
	//LoadDataListener _activity;	
	public Handler mHandle = new Handler()
	{
	  
		public void handleMessage(Message paramAnonymousMessage)
		  {
			Context _context =  GlobalApplication._();
		    switch (paramAnonymousMessage.what)
		    {
		    	case 0:
		    		//success
		    		//pbarDialog.dismiss();
		    		Log.v("start List Acitivity");
		    		_dialog.hide();		    	
					Intent mIntent = new Intent(_context, ListActivity.class);
					mIntent.putExtra(Contants.EXTRA_LIST_TYPE, Contants.LIST_TYPE_USER);	
					_activity.startActivity(mIntent);									
					_activity.overridePendingTransition(R.anim.slide_in_right, R.anim.no_animation);
		    		break;
		    	case 1:
		    		_dialog.hide();
		    		Log.v("start Category");
		    		Intent startCat = new Intent(_context,CategoryActivity.class);
		    		startCat.putExtra(Contants.EXTRA_CATEGORY_TYPE, Contants.CATEGORY_TYPE_USER_PAPER);
					_activity.startActivity(startCat);									
					_activity.overridePendingTransition(R.anim.slide_in_right, R.anim.no_animation);
					
		    		break;
		    	case 2:	    		
		    		//RestClient._().getHttpRequest().getConnection().disconnect();
		    		//NetworkAPI._()._loaderThread.interrupt(); 
		    		_dialog.hide();	    	
		    		break;
		    }
		  }
	};
	public onUserPaperClickListenner(int itemindex,Context context,Activity activity)
	{
		_itemIndex = itemindex;		
		_activity = activity;
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//NetworkAPI._currentPaperIndex = 
		Paper pp = NetworkAPI._userListPapers.get(_itemIndex);
		String pp_id = pp.getId();
		NetworkAPI._currentPaperId = pp_id; 		
		NetworkAPI._currentPaperIndex = _itemIndex; 

		//TRACKING USER PAPER CLICK
		EasyTracker.getInstance().setContext(GlobalApplication._());			
	    Tracker tracker = EasyTracker.getTracker();
		tracker.sendEvent(Contants.TRACKER_USER_PAPER_BUTTONS,NetworkAPI._currentPaperId + "-" + Contants.TRACKER_ACTIONS_CLICKED,pp.getTitle() , Utils.currentMillis());

		
		List<Category> categories = pp.getCategories();	
		//_dialog  = new LoadingDialog(v.getContext(), mHandle,2);
		_dialog = new LoadingDialog(v.getContext());
		_dialog.show();
		NetworkAPI._()._loaderThread.setloadDataListener(this);			
		//this._mRequest = NetworkAPI._()._loaderThread;
		Log.v("categories.size():" + categories.size());
		
		if(categories.size() == 1)
		{
			NetworkAPI._currentCategoryId = categories.get(0).getId();
			
			NetworkAPI.onSetRequest(Contants.MSG_LOAD_LIST);
		}
		else
		{
			//jump to list category
			//this._mRequest.onSetRequest(Contants.MSG_LOAD_CAT);
			this.mHandle.sendEmptyMessage(1);
		}
	
		
	}
	
	@Override
	public void onPaperloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCategoryloadSuccess() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(1);
	}

	@Override
	public void onListloadSuccess() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onListloadFail() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUserloadSuccess() {
		// TODO Auto-generated method stub
		
	}

}
